package com.hdax.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdax.entity.Permission;
import com.hdax.entity.Role;
import com.hdax.service.impl.RoleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleServiceImpl service;

    //    打开角色管理页面
    @GetMapping("/open")
    public ModelAndView openRole(){
        return new ModelAndView("role/list");
    }

    //    查询所有角色
    @RequestMapping("/list")
    public IPage<Role> selectRole(Role role,Integer page,Integer limit){
        Page<Role> p=new Page<>(page,limit);
        return service.selectPageVo(p,role);
    }

    //    跳转到添加修改页面
    @RequestMapping("/addOrUpdateRolePage")
    public ModelAndView openAddPage(Integer id){
        ModelAndView mv=new ModelAndView("role/addAndUpdate");
        if(id!=null){
            Role role = service.selectById(id);
            mv.addObject("role",role);
        }
        return mv;
    }

    //角色添加
    @PostMapping("/save")
    public int addRole(Role role){
        role.setCreateTime(LocalDateTime.now());
        return service.addRole(role);
    }

    //    角色修改
    @PostMapping("/update")
    public int updateRole(Role role){
        role.setUpdateTime(LocalDateTime.now());
        return service.updateRole(role);
    }

    //    角色删除
    @PostMapping("/delete")
    public int deleteRole(Integer id){
        return service.deleteRole(id);
    }

}

